package com.purplesoft.zettl.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.purplesoft.zettl.R;
import com.purplesoft.zettl.Database.DatabaseExecuter;
import com.purplesoft.zettl.DatabaseTables.TblItems;
import com.purplesoft.zettl.DatabaseTables.TblManufacturer;

public class EditManufacturer extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {

	private ListView lvManufacturer;
	private ImageButton btAddNewManufacturer;
	private TextView tvManufacturer, tvAddNewManufacturer;
	private EditText etManufacturer;
	private Button btCancel;
	private Cursor csTblManufacturer;
	private SimpleCursorAdapter laManufacturer;
	private Dialog editManufacturerDialog;
	private AlertDialog alertDialogManufacturer;
	private DatabaseExecuter dbExec;
	private String manufacturer;
	private final int OK_BUTTON = 1, YES_NO_BUTTON = 2;
	private final int MANUFACTURER_ID_DEFAULT_VALUE = -99;
	private final String EQUAL_OPERATION = " = ", NOT_EQUAL_OPERATION = " != ";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(R.style.Theme_Purple);
		setContentView(R.layout.edit_manufacturer);
        
		lvManufacturer = (ListView) findViewById(R.id.lvManufacturer_EditManufacturer);
		btAddNewManufacturer = (ImageButton) findViewById(R.id.btAddManufacturer_EditManufacturer);
		tvAddNewManufacturer = (TextView) findViewById(R.id.tvAddNewManufacturer_EditManufacturer);
		btCancel = (Button) findViewById(R.id.btCancel_EditManufacturer);
		
		dbExec = new DatabaseExecuter(this);
		
		selectDataForLvLists();
		btAddNewManufacturer.setOnClickListener(this);
		tvAddNewManufacturer.setOnClickListener(this);
		btCancel.setOnClickListener(this);
	}

	private void selectDataForLvLists() {
		csTblManufacturer = dbExec.queryFromTblManufacturer(TblManufacturer.Columns._ID, MANUFACTURER_ID_DEFAULT_VALUE, NOT_EQUAL_OPERATION);
		startManagingCursor(csTblManufacturer);
		laManufacturer = new SimpleCursorAdapter(this, R.layout.edit_manufacturer_row, csTblManufacturer, new String[] { TblManufacturer.Columns.Manufacturer }, new int[] { R.id.tvManufacturer_EditManufacturerRow });
		lvManufacturer.setAdapter(laManufacturer);
	}

	public void onClick(View view) {
		switch(view.getId()) {
		case R.id.btAddManufacturer_EditManufacturer:
			showEditManufacturerDialog(null);
			break;
		case R.id.tvAddNewManufacturer_EditManufacturer:
			showEditManufacturerDialog(null);
			break;
		case R.id.btCancel_EditManufacturer:
			finish();
			break;
		case R.id.btSave_EditManufacturerDialog:
			saveManufacturer();
			break;
		case R.id.btCancel_EditManufacturerDialog:
			manufacturer = null;
			editManufacturerDialog.dismiss();
			break;
		}
	}
	
	public void selectManufacturer(View view) {
		RelativeLayout editManufacturerRow = (RelativeLayout) view.getParent();
		tvManufacturer = (TextView) editManufacturerRow.findViewById(R.id.tvManufacturer_EditManufacturerRow);
		manufacturer = tvManufacturer.getText().toString();
		csTblManufacturer = dbExec.queryFromTblManufacturer(TblManufacturer.Columns.Manufacturer, manufacturer, EQUAL_OPERATION);
		startManagingCursor(csTblManufacturer);
		Bundle resultBundle = new Bundle();
		resultBundle.putString(TblManufacturer.Columns.Manufacturer, manufacturer);
		resultBundle.putInt(TblManufacturer.Columns._ID, csTblManufacturer.getInt(csTblManufacturer.getColumnIndex(TblManufacturer.Columns._ID)));
		Intent resultIntent = new Intent().putExtras(resultBundle);
		setResult(RESULT_OK, resultIntent);
		finish();
	}
	
	public void editManufacturer(View view) {
		RelativeLayout editManufacturerRow = (RelativeLayout) view.getParent();
		tvManufacturer = (TextView) editManufacturerRow.findViewById(R.id.tvManufacturer_EditManufacturerRow);
		manufacturer = tvManufacturer.getText().toString();
		showEditManufacturerDialog(manufacturer);
	}

	public void deleteManufacturer(View view) {
		RelativeLayout editManufacturerRow = (RelativeLayout) view.getParent();
		tvManufacturer = (TextView) editManufacturerRow.findViewById(R.id.tvManufacturer_EditManufacturerRow);
		manufacturer = tvManufacturer.getText().toString();
		showAlertDialog(getResources().getString(R.string.delete_manufacturer), YES_NO_BUTTON);
	}
	
	private boolean checkIfAllFieldsAreFilled() {
		if (etManufacturer.getText().toString().equals("")) {
			showAlertDialog(getResources().getString(R.string.fill_in_manufacturer), OK_BUTTON);
			return false;
		} else {
			return true;
		}
	}

	private void saveManufacturer() {
		if (checkIfAllFieldsAreFilled() == false) return;
		if (manufacturer !=null) {
			if (dbExec.updateTblManufacturer(manufacturer, etManufacturer.getText().toString()) == false) {
				showAlertDialog(getResources().getString(R.string.manufacturer_already_exists), OK_BUTTON);
				etManufacturer.selectAll();
				return;
			}
		} else {
			if (dbExec.insertIntoTblManufacturer(etManufacturer.getText().toString()) == false) {
				showAlertDialog(getResources().getString(R.string.manufacturer_already_exists), OK_BUTTON);
				etManufacturer.selectAll();
				return;
			}
		}
			manufacturer = null;
			selectDataForLvLists();
			editManufacturerDialog.dismiss();
	}

	private void showAlertDialog(String alertText, int buttons) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		switch(buttons) {
		case OK_BUTTON:
			builder.setMessage(alertText);
			builder.setNeutralButton(R.string.ok, this);
			break;
		case YES_NO_BUTTON:
			builder.setMessage(String.format(alertText, manufacturer));
			builder.setCancelable(false);
		    builder.setPositiveButton(R.string.yes, this);
		    builder.setNegativeButton(R.string.no, this);
		    break;
		}
		if (alertDialogManufacturer == null || !alertDialogManufacturer.isShowing()) {
			alertDialogManufacturer = builder.create();
			alertDialogManufacturer.show();
		}
	}

	private void showEditManufacturerDialog(String manufacturer) {
		RelativeLayout viewGroup = (RelativeLayout) findViewById(R.id.EditManufacturerDialog);
		LayoutInflater layoutInfater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = layoutInfater.inflate(R.layout.edit_manufacturer_dialog, viewGroup);
		
		if (editManufacturerDialog == null || !editManufacturerDialog.isShowing()) {
			editManufacturerDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
			editManufacturerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			editManufacturerDialog.setContentView(layout);
			
			WindowManager.LayoutParams lp = editManufacturerDialog.getWindow().getAttributes();
			lp.dimAmount=0.6f;
			editManufacturerDialog.getWindow().setAttributes(lp);  
			editManufacturerDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
			editManufacturerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
			
			Button btSaveEditManufacturerDialog = (Button) editManufacturerDialog.findViewById(R.id.btSave_EditManufacturerDialog);
			Button btCancelEditManufacturerDialog = (Button) editManufacturerDialog.findViewById(R.id.btCancel_EditManufacturerDialog);
			TextView tvTitleEditManufacturerDialog = (TextView) editManufacturerDialog.findViewById(R.id.tvTitle_EditManufacturerDialog);
			etManufacturer = (EditText) editManufacturerDialog.findViewById(R.id.etManufacturer_EditManufacturerDialog);
			
			if (manufacturer != null) {
				tvTitleEditManufacturerDialog.setText(getResources().getString(R.string.edit_manufacturer));
				etManufacturer.setText(manufacturer);
				etManufacturer.selectAll();
				btSaveEditManufacturerDialog.setText(R.string.alter);
			}
			
			btSaveEditManufacturerDialog.setOnClickListener(this);
			btCancelEditManufacturerDialog.setOnClickListener(this);
			
			editManufacturerDialog.show();
		}
	}

	public void onClick(DialogInterface dialog, int which) {
		switch(which) {
		case -1: // Yes
				csTblManufacturer = dbExec.queryFromTblManufacturer(TblManufacturer.Columns.Manufacturer, manufacturer, EQUAL_OPERATION);
				startManagingCursor(csTblManufacturer);
				dbExec.updateTblItems(TblItems.Columns.ManufacturerID, csTblManufacturer.getInt(csTblManufacturer.getColumnIndex(TblManufacturer.Columns._ID)), MANUFACTURER_ID_DEFAULT_VALUE);
				dbExec.deleteFromTblManufacturer(manufacturer);
				selectDataForLvLists();
				Toast.makeText(this, String.format(getResources().getString(R.string.manufacturer_deleted), manufacturer), Toast.LENGTH_LONG).show();
				manufacturer = null;
		}
	}
}
